package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bomp.facade.base.Header;
import cc.rengu.igas.bomp.facade.bean.MchntApexInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntBaseInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntContactsInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntBaseInfoManageRequest;
import cc.rengu.igas.bomp.facade.response.MchntBaseInfoManageResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntBaseManageRequest;
import cc.rengu.igas.bsps.facade.response.MchntBaseManageResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;

import java.util.ArrayList;
import java.util.List;

public class MchntBaseManageService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntBaseManageRequest mchntBaseManageRequest = new MchntBaseManageRequest();
        ConvertUtil.convertOutput(mchntBaseManageRequest);
        return mchntBaseManageRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntBaseManageRequest mchntBaseManageRequest = (MchntBaseManageRequest) request;
        BizResponse bizResponse = new BizResponse();
        MchntBaseManageResponse response = new MchntBaseManageResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        String instId = mchntBaseManageRequest.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(instId)) {
            rglog.error("必传参数instId法人号为空！", instId);
            bizResponse.setRespCode(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
/**
 * 根据请求参数决定响应类型，这里是判断是否根据商户号查询
 *
 * */
        String mchntNo = getMchntNo(mchntBaseManageRequest);
        if (StringUtil.isEmptyOrNull(mchntNo)) {
            bizResponse.setResult(response);
            bizResponse.setRespCode(RespCodeEnum.MCHNT_BASE_INFO_MANAGE_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.MCHNT_BASE_INFO_MANAGE_ERROR.getRespDesc());
            return bizResponse;
        }
        response.setHeader(mchntBaseManageRequest.getHeader());
        response.setMchntNo(mchntNo);
        bizResponse.setResult(response);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntBaseManageResponse mchntBaseManageResponse = (MchntBaseManageResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntBaseManageResponse);
    }

    public String getMchntNo(MchntBaseManageRequest mchntBaseManageRequest) throws Exception {
        String callMode = "1";
        callMode = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.APPLICATION_CONF_NODE, AppConfigConstant.BOMP_SERVICE_CALL_MODE);
        if (!StringUtil.isEmptyOrNull(callMode)) {
            if (!"0".equals(callMode.trim()) && !"1".equals(callMode.trim())) {
                rglog.error("内管相关信息获取调用模式<{}>参数配置错误:0-本地调用，1-微服务调用!", callMode.trim());
                throw new BizException("内管相关信息获取方式配置有误:<{}>", callMode);
            } else {
                callMode = callMode.trim();
            }
        }
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.info("正在进行商户基本信息管理", "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.HEADER_SRC_SYS_ID));
        header.setChanlId(ChannelEnum.UPMP.getChannelType());
        header.setTxnNum("M00BM011");
        header.setBizType("00");
//        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        MchntBaseInfoManageRequest mchntBaseInfoManageRequest = new MchntBaseInfoManageRequest();
        mchntBaseInfoManageRequest.setHeader(header);
        mchntBaseInfoManageRequest.setUserId(mchntBaseManageRequest.getUserId());
        MchntBaseInfoBean mchntBaseInfoBean = new MchntBaseInfoBean();
        MchntContactsInfoBean mchntContactsInfoBean = new MchntContactsInfoBean();
        List<MchntApexInfoBean> mchntApexInfoBeanList = new ArrayList<>();
        BeanUtil.beanCopy(mchntBaseManageRequest.getMchntBaseInfoBean(), mchntBaseInfoBean);
        BeanUtil.beanCopy(mchntBaseManageRequest.getMchntContactsInfoBean(), mchntContactsInfoBean);
        mchntBaseInfoManageRequest.setMchntBaseInfoBean(mchntBaseInfoBean);
        mchntBaseInfoManageRequest.setMchntContactsInfoBean(mchntContactsInfoBean);
        mchntBaseInfoManageRequest.setMchntApexInfoBeanList(mchntApexInfoBeanList);
        mchntBaseInfoManageRequest.setOprType(mchntBaseManageRequest.getOprType());
        mchntBaseInfoManageRequest.setOprMenuId(mchntBaseManageRequest.getOprMenuId());
        mchntBaseInfoManageRequest.setCheckStatus(mchntBaseManageRequest.getCheckStatus());
        mchntBaseInfoManageRequest.setChecked(mchntBaseManageRequest.getChecked());
        mchntBaseInfoManageRequest.setAuditRefuseReason(mchntBaseManageRequest.getAuditRefuseReason());
        mchntBaseInfoManageRequest.setOprMenuName(mchntBaseManageRequest.getOprMenuName());
        mchntBaseInfoManageRequest.setWebNextUrl(mchntBaseManageRequest.getWebNextUrl());
        mchntBaseInfoManageRequest.setMasterShowFlag(mchntBaseManageRequest.getMasterShowFlag());
        Result<MchntBaseInfoManageResponse> callResult = null;
        DubboService dubboService = new DubboServiceImpl();
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BOMP_MCHNT_MANAGE_SERVICE_INNER_PATH);
            callResult = (Result<MchntBaseInfoManageResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BOMP_MCHNT_BASE_INFO_MANAGE_METHOD, mchntBaseInfoManageRequest);
        } else {
            callResult = (Result<MchntBaseInfoManageResponse>) dubboService.callDubboService(ShareServiceConstant.BOMP_MCHNT_MANAGE_SERVICE, ShareServiceConstant.BOMP_MCHNT_BASE_INFO_MANAGE_METHOD, mchntBaseInfoManageRequest);
        }
        if (null == callResult || null == callResult.getResult() || null == callResult.getResult().getMchntNo()) {
            rglog.info("进行商户管理失败,mchntNo:<{}>");
            return null;
        }
        String mchntNo = callResult.getResult().getMchntNo();
        return mchntNo;
    }
}
